package grp.test.controller;


import grp.test.common.Const;
import grp.test.common.ResponseCode;
import grp.test.common.ServerResponse;
import grp.test.pojo.Book;
import grp.test.pojo.User;
import grp.test.service.IInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/information/")
public class InformationController {

    @Autowired
    private IInformationService iInformationService;


    @RequestMapping(value = "get_book_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Book> get_book_information(Integer bookId){
        if(bookId == null){
            return ServerResponse.createByErrorMessage("查询数据编号不能为空！");
        }
        return iInformationService.getBookInformation(bookId);
    }

    @RequestMapping(value = "update_book_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Book> update_book_information(HttpSession session, Book book){
        User currentUser =(User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"管理员未登录,需要强制登录！");
        }
        else if (currentUser.getUserType()!= Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("你没有管理员权限！");
        }
        if(book == null){
            return ServerResponse.createByErrorMessage("更新数据不能为空！");
        }
        return iInformationService.updateBookInformation(book);
    }
}
